package controller

import (
	"com.haojuetrace/actualCombat/model"
	"com.haojuetrace/actualCombat/repository"
	"com.haojuetrace/actualCombat/response"
	"com.haojuetrace/actualCombat/vo"
	"github.com/gin-gonic/gin"
	"log"
)

/*
文章：
	1.创建
	2.删除
	3.修改
	4.查看
	5.分页
逻辑代码
*/

type IPostController interface {
	RestController
	List(ctx *gin.Context)
}

type PostController struct {
	repository repository.IPostRepository
}

//NewPostController 创建PostController实体
func NewPostController() IPostController {

	return PostController{repository: repository.NewPostRepository()}
}

//Create 创建文章
func (p PostController) Create(ctx *gin.Context) {
	var reqPost vo.PostVo
	//数据验证
	if err := ctx.ShouldBind(&reqPost); err != nil {
		log.Println(err.Error())
		response.Fail(ctx, nil, "数据验证错误")
		return
	}
	//获取登录用户 user
	user, _ := ctx.Get("user")
	//创建post
	post := &model.Post{
		Userid:     user.(*model.Users).Id,
		ClassifyId: reqPost.ClassifyId,
		Title:      reqPost.Title,
		HeadImg:    reqPost.HeadImg,
		Content:    reqPost.Content,
	}
	err := p.repository.Create(post)
	if err != nil {
		response.Fail(ctx, nil, "创建失败，系统繁忙")
		return
	}
	response.Success(ctx, gin.H{"post": post}, "创建成功")

}

//Delete 删除文章
func (p PostController) Delete(ctx *gin.Context) {
	//获取path中的id
	postId := ctx.Params.ByName("id")

	exist, post := p.repository.Select("id", postId)
	if !exist {
		response.Fail(ctx, nil, "文章不存在")
		return
	}
	//获取登录用户 user
	user, _ := ctx.Get("user")
	if user.(*model.Users).Id != post.Userid {
		response.Fail(ctx, nil, "权限不足")
		return
	}
	if err := p.repository.Delete(post); err != nil {
		response.Fail(ctx, nil, "删除失败")
		return
	}
	response.Success(ctx, nil, "文章删除成功")
}

//Updata 修改文章
func (p PostController) Updata(ctx *gin.Context) {
	var reqPost vo.PostVo
	//数据验证
	if err := ctx.ShouldBind(&reqPost); err != nil {
		log.Println(err.Error())
		response.Fail(ctx, nil, "数据验证错误")
		return
	}
	//获取path中的id
	postId := ctx.Params.ByName("id")

	exist, post := p.repository.Select("id", postId)
	if !exist {
		response.Fail(ctx, nil, "文章不存在")
		return
	}

	//获取登录用户 user
	user, _ := ctx.Get("user")
	if user.(*model.Users).Id != post.Userid {
		response.Fail(ctx, nil, "权限不足")
		return
	}
	post.Title = reqPost.Title
	post.Content = reqPost.Content
	post.HeadImg = reqPost.HeadImg
	post.ClassifyId = reqPost.ClassifyId

	if _, err := p.repository.Updata(post); err != nil {
		log.Println(err.Error())
		response.Fail(ctx, nil, "文章修改失败")
		return
	}
	response.Success(ctx, nil, "修改成功")

}

//Show 查看文章
func (p PostController) Show(ctx *gin.Context) {

	//获取path中的id
	postId := ctx.Params.ByName("id")

	exist, post := p.repository.Select("id", postId)
	if !exist {
		response.Fail(ctx, nil, "文章不存在")
		return
	}
	response.Success(ctx, gin.H{"post": post}, "")
}

//List 列出分页文章
func (p PostController) List(ctx *gin.Context) {

	var reqPostList vo.PostListVo
	//数据验证
	if err := ctx.ShouldBind(&reqPostList); err != nil {
		log.Println(err.Error())
		response.Fail(ctx, nil, "数据验证错误")
		return
	}
	posts, count, err := p.repository.List(reqPostList.Pages, reqPostList.Num)
	if err != nil {
		response.Fail(ctx, nil, "查询失败，系统繁忙")
		return
	}
	response.Success(ctx, gin.H{"post": posts, "count": count}, "")
}
